package com.anuo.app.study.studydesignpatterns;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations = {"classpath:spring/spring-context.xml"})
/**
 * 状态设计模式
 * 应用场景：在对象内部状态（如下面的Content对象的内部状态为state）改变时, 改变他的行为.
 */
public class StudyState {

    @Test
    public void studyState() {
        Context context = new Context();
        context.setState(new Sunshine());
        System.out.println(context.getStateMsg());
        context.setState(new Rain());
        System.out.println(context.getStateMsg());

    }

    public interface State {
        String getStateMsg();
    }

    private class Sunshine implements State {

        @Override
        public String getStateMsg() {
            return "晴天";
        }
    }

    private class Rain implements State {

        @Override
        public String getStateMsg() {
            return "雨天";
        }
    }

    private class Context {
        private State state;

        public void setState(State state) {
            this.state = state;
        }

        public String getStateMsg() {
            return state.getStateMsg();
        }
    }
}
